package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.domain.VehicleCancellation;
import com.ruoyi.wms.dto.VehicleCancellationDto;
import com.ruoyi.wms.mapper.VehicleCancellationMapper;
import com.ruoyi.wms.service.IVehicleCancellationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 机动车注销登记申请Service业务层处理
 *
 * @author libancang
 * @date 2025-09-12
 */
@Service
public class VehicleCancellationServiceImpl implements IVehicleCancellationService {
    @Autowired
    private VehicleCancellationMapper vehicleCancellationMapper;

    /**
     * 查询机动车注销登记申请
     *
     * @param id 机动车注销登记申请主键
     * @return 机动车注销登记申请
     */
    @Override
    public VehicleCancellation selectVehicleCancellationById(String id) {
        return vehicleCancellationMapper.selectVehicleCancellationById(id);
    }

    /**
     * 查询机动车注销登记申请列表
     *
     * @param vehicleCancellation 机动车注销登记申请
     * @return 机动车注销登记申请
     */
    @Override
    public List<VehicleCancellation> selectVehicleCancellationList(VehicleCancellationDto vehicleCancellation) {
        return vehicleCancellationMapper.selectVehicleCancellationList(vehicleCancellation);
    }

    /**
     * 新增机动车注销登记申请
     *
     * @param vehicleCancellation 机动车注销登记申请
     * @return 结果
     */
    @Override
    public int insertVehicleCancellation(VehicleCancellation vehicleCancellation) {

        vehicleCancellation.setId(MD5Utils.md5(vehicleCancellation.getPlateNo()));
        vehicleCancellation.setCreateUser(SecurityUtils.getUsername());
        vehicleCancellation.setCreateDate(DateUtils.getCurrentTime());
        vehicleCancellation.setUpdateDate( DateUtils.getCurrentTime());

        return vehicleCancellationMapper.insertVehicleCancellation(vehicleCancellation);
    }

    /**
     * 修改机动车注销登记申请
     *
     * @param vehicleCancellation 机动车注销登记申请
     * @return 结果
     */
    @Override
    public int updateVehicleCancellation(VehicleCancellation vehicleCancellation) {
        int updateResult = 0;
        if (StringUtils.isEmpty(vehicleCancellation.getId())) {
            updateResult = insertVehicleCancellation(vehicleCancellation);
        }
        vehicleCancellation.setUpdateDate( DateUtils.getCurrentTime());
        updateResult = vehicleCancellationMapper.updateVehicleCancellation(vehicleCancellation);
        if (0 == updateResult) {
            updateResult = insertVehicleCancellation(vehicleCancellation);
        }

        return updateResult;
    }

    /**
     * 批量删除机动车注销登记申请
     *
     * @param ids 需要删除的机动车注销登记申请主键
     * @return 结果
     */
    @Override
    public int deleteVehicleCancellationByIds(String[] ids) {
        return vehicleCancellationMapper.deleteVehicleCancellationByIds(ids);
    }

    /**
     * 删除机动车注销登记申请信息
     *
     * @param id 机动车注销登记申请主键
     * @return 结果
     */
    @Override
    public int deleteVehicleCancellationById(String id) {
        return vehicleCancellationMapper.deleteVehicleCancellationById(id);
    }

    @Override
    public VehicleCancellation getInfoByplateNo(String plateNo) {


        VehicleCancellation infoByplateNo = vehicleCancellationMapper.getInfoByplateNo(plateNo);
        return infoByplateNo;
    }
}
